package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Rodgers
 * @Date 2022/3/14 11:39
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 添加套餐的业务方法
     *
     * @param setmealDto
     * @return
     */
    @Transactional  //开启事务
    @Override
    public boolean save(SetmealDto setmealDto,HttpSession session) {
        //1.获取登录人
        Long id = (Long) session.getAttribute("employee");

        //2.完善套餐数据
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);

        //完善套餐数据(状态、创建时间、更新时间)
        setmealDto.setStatus(1);  //启用
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());

        //2.调用数据访问层新增套装(注意新增后获取主键值)
        setmealMapper.save(setmealDto);

        //3.遍历套餐菜品关系列表完善数据
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map((setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setSort(0);
            return setmealDish;
        })).collect(Collectors.toList());

        //4.调用数据访问批量插入套餐菜品列表数据
        setmealDishMapper.save(setmealDishList);

        return true;
    }

    /**
     * 套餐的分页查询数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //1.pageHelper设置查询第几页和每页大小
        PageHelper.startPage(page,pageSize);

        //2.执行查询所有的套餐数据
        //经过pageHelper处理以后返回当前页数据列表
        List<Setmeal> setmealList = setmealMapper.findAll(name);

        //3.封装层PageInfo对象
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        //4.处理当前页数据列表中每个套餐setmeal对象
        //根据里面的类别id查询类别对象，最后将所有数据封装给SetmealDto
        //将List<Setmeal>当前页数据列表=》List<SetmealDto>
        List<SetmealDto> setmealDtoList = setmealList.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();

            //将setmeal套餐对象的数据赋值给setmealDto里面属性数据
            BeanUtils.copyProperties(setmeal,setmealDto);

            //获取类别对象
            Category category = categoryMapper.findById(setmealDto.getCategoryId());

            //将类别名称赋值给setmealDto
            setmealDto.setCategoryName(category.getName());

            return setmealDto;
        })).collect(Collectors.toList());

        //5.封装层前端需要Page<SetmealDto>对象
        Page<SetmealDto> pageDto = new Page<>();
        pageDto.setRecords(setmealDtoList);
        pageDto.setTotal(pageInfo.getTotal());

        return pageDto;
    }

    /**
     * 删除套餐业务方法
     *
     * @param ids
     * @param updateUser
     * @return
     */
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        //1.判断传递过来的套餐id列表查询是否存在启用的套餐
        //查询启动的个数，个数大于0代表存在，否则不存在
        Integer count = setmealMapper.countByIds(ids);

        //2.如果存在，抛出异常“无法删除起售的套餐”
        if (count>0){
            throw new BusinessException("无法删除起售的套餐");
        }

        //3.调用数据访问层进行批量删除，传递修改人，最新的修改时间
        //批量删除套餐菜品关系数据
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());

        //批量删除套餐
        setmealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());

        return true;
    }

}
